package sj;

public class NodeList {
    public NodeList.Node head;

    //尾插入
    public void lastAdd(Node newNode) {
        Node tailNode = getLast();
        tailNode.next = newNode;
    }

    //获取tail
    public Node getLast() {
        Node result;
        result = this.head;
        while (result.next != null) {
            result = result.next;
        }
        return result;
    }

    //获取size
    public int size() {
        Node current;
        current = this.head;

        int i = 1;
        while (current.next != null) {
            current = current.next;
            i++;
        }

        return i;
    }

    public NodeList(NodeList.Node head) {
        this.head = head;
    }

    //内部类
    static class Node {
        public String name;
        public Node next;

        public Node(String name) {
            this.name = name;
        }
    }

    public static void main(String[] args) {
        Node node1 = new Node("1");
        Node node2 = new Node("2");
        Node node3 = new Node("3");
        //1→ 2→ 3
        NodeList list = new NodeList(node1);
        list.lastAdd(node2);
        list.lastAdd(node3);


        System.out.println(list.size());
    }
}
